package org.jarraiders.musiclibrary.client.view;

import java.util.ArrayList;
import java.util.List;

import org.jarraiders.musiclibrary.client.presenter.ArtistsPresenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ArtistsView extends Composite implements ArtistsPresenter.Display {
	private final Button addButton;
	private FlexTable artistsTable;
	private final FlexTable contentTable;
	private Button searchButton;
	private TextBox searchTextBox;
	private int indexOfPopup;
	private HasClickHandlers deleteButton;
	private ArtistsPresenter presenter;
	
	public ArtistsView() {
		
		DecoratorPanel contentTableDecorator = new DecoratorPanel();
		initWidget(contentTableDecorator);
		contentTableDecorator.setWidth("100%");
		contentTableDecorator.setWidth("18em");

		contentTable = new FlexTable();
		contentTable.setWidth("100%");
		contentTable.getCellFormatter().addStyleName(0, 0, "library-ListContainer");
		contentTable.getCellFormatter().setWidth(0, 0, "100%");
		contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);

		
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setBorderWidth(0);
		hPanel.setSpacing(0);
		hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		addButton = new Button("Add");
		hPanel.add(addButton);
		deleteButton = new Button("Delete");
		
		contentTable.getCellFormatter().addStyleName(0, 0, "library-ListMenu");
		contentTable.setWidget(0, 0, hPanel);

		HorizontalPanel searchPanel=new HorizontalPanel();
		searchButton=new Button("Search");
		searchTextBox=new TextBox();
		searchPanel.add(searchTextBox);			
		searchPanel.add(searchButton);
		contentTable.setWidget(1, 0, searchPanel);
		
		artistsTable = new FlexTable();
		artistsTable.setCellSpacing(0);
		artistsTable.setCellPadding(0);
		artistsTable.setWidth("100%");
		artistsTable.addStyleName("library-ListContents");
		artistsTable.getColumnFormatter().setWidth(0, "15px");
		contentTable.setWidget(2, 0, artistsTable);
		
		contentTableDecorator.add(contentTable);
	}

	public void setPresenter(ArtistsPresenter presenter){
		this.presenter=presenter;
	}
	
	@Override
	public HasClickHandlers getAddButton() {
		return addButton;
	}

	@Override
	public HasClickHandlers getDeleteButton() {
		
		return deleteButton;
	}

	@Override
	public HasClickHandlers getList() {
		
		return artistsTable;
	}

	@Override
	public void setData(final List<String> data) {
		artistsTable.removeAllRows();
		indexOfPopup=-1;

		for (int i = 0; i < data.size(); ++i) {
			
			final ArtistsIcon artistIcon = new ArtistsIcon(i, presenter);
			
			artistIcon.setText(data.get(i));
			
			final int pushedIndex=i;
			artistIcon.addMouseOverHandler(new MouseOverHandler() {

				@Override
				public void onMouseOver(MouseOverEvent event) {
					setPopup(pushedIndex, artistIcon);
					
				}
			});
			
			artistIcon.addMouseOutHandler(new MouseOutHandler() {
				
				@Override
				public void onMouseOut(MouseOutEvent event) {
					if (indexOfPopup!=pushedIndex){
						//((ArtistsIcon)(artistsTable.getWidget(indexOfPopup/6, indexOfPopup%6))).setPopupUnvisible();
					}
					
				}
			});
			
			artistsTable.setWidget(i/6, i%6, artistIcon);
			
		}
	}

	@Override
	public int getClickedRow(ClickEvent event) {
		int selectedRow = -1;
	    HTMLTable.Cell cell = artistsTable.getCellForEvent(event);
	    
	    if (cell != null) {
	      selectedRow = cell.getCellIndex();
	      }
	    
	    
	    return selectedRow;
	}

	@Override
	public Integer getSelectedArtist() {
		  
	    return indexOfPopup;
	}
	@Override
	public Widget asWidget() {
	    return this;
	  }
	
	private void setPopup(int index, ArtistsIcon artistIcon){
		if (indexOfPopup!=-1){					
			((ArtistsIcon)(artistsTable.getWidget(indexOfPopup/6, indexOfPopup%6))).setPopupUnvisible();
		}
		artistIcon.setPopupVisible();
		indexOfPopup=index;
	}

	@Override
	public HasClickHandlers getSearchButton() {
		
		return searchButton;
	}

	@Override
	public String getSearchText() {
		
		return searchTextBox.getText();
	}
	

}
